package com.sofwin;

import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.FilterRegistration;
import javax.servlet.ServletException;

/**
 * @packageName: com.sofwin
 * @user: andyliu
 * @date: 2021/7/9 9:43
 * @email 115176513@qq.com
 * @description: TODO
 */

public class WebServlet extends AbstractAnnotationConfigDispatcherServletInitializer  {
    // 类对象数组
    // appplicationContext.xml的对象数组
    //  包含有@Configuration注解的类的类对象数组
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{RootConfiger.class,MybatisConfiter.class};
    }

    // spring mvc的配置的类对象数组
    // 实现接口WebMvcConfigurer
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{MvcConfiger.class};
    }
    // 设置web.xml中url-pattern
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    // 可以配置过滤器
    @Override
    public void onStartup(javax.servlet.ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);
        // 相当于在web.xml中配置filter标签
        FilterRegistration.Dynamic charactor = servletContext.addFilter("charactor", CharacterEncodingFilter.class);
       // <init-param>
        charactor.setInitParameter("encoding","utf-8");
        // filter-mapping
        charactor.addMappingForUrlPatterns(null,false,"/*");

        FilterRegistration.Dynamic shiroFilter = servletContext.addFilter("shiroFilter", DelegatingFilterProxy.class);
        shiroFilter.addMappingForUrlPatterns(null,false,"/*");

    }
}
